package com.linln.wtp.controller.area;

import com.linln.common.utils.GeoUtil;
import com.linln.component.wtpshiro.annotation.PassToken;
import com.linln.modules.area.domain.Area;
import com.linln.modules.area.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author : pengliang
 * @version V1.0
 * @Project: timo
 * @Package com.linln.wtp.controller.area
 * @Descriptio /**
 * @Project: timo
 * @Package com.linln.wtp.controller.area
 * @Description: TODO
 * @date Date : 2020年11月30日 14:08
 */
@RestController
@RequestMapping("/area")
public class AreaController {
    @Autowired
    private AreaService areaService;

    //获取全部的城市
    @PassToken
    @GetMapping
    public Map<String, Object> getArea(String keyword) {
        Map<String, Object> map = new HashMap<>();
        try {
            map.put("data",areaService.getAreaGroupByInitials(keyword));
            map.put("code", 200);
            map.put("msg", "获取城市列表成功");
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "获取城市列表失败");
        }
        return map;
    }

    //通过经度纬度获取城市名称
    @PassToken
    @GetMapping("/cityByAddress")
    public Map<String, Object> getCityByAddress(@RequestParam("lon") String lon, @RequestParam("lat") String lat) {
        Map<String, Object> map = new HashMap<>();
        String cityByAddress = GeoUtil.getLatAndLng(lon, lat).getJSONObject("address_component").getString("city");
        try {
            Area area = areaService.getAreaByName(cityByAddress);
            map.put("code", 200);
            map.put("data", area);
            map.put("msg", "获取城市成功");
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "获取城市失败");
        }
        return map;
    }

    //通过经度纬度获取详细地址
    @PassToken
    @GetMapping("/cityByInfoAddress")
    public Map<String, Object> getCityByInfoAddress(@RequestParam("lon") String lon, @RequestParam("lat") String lat) {
        Map<String, Object> map = new HashMap<>();
        String cityByAddress = GeoUtil.getLatAndLng(lon, lat).getJSONObject("formatted_addresses").getString("recommend");
        try {
            map.put("code", 200);
            map.put("data", cityByAddress);
            map.put("msg", "获取城市成功");
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "获取城市失败");
        }
        return map;
    }
}
